<template>
  <div class="app-container flex-col minH">
    <div class="topline w100p bgwhite pt20 pb10 radius10 pl10 pr20 justify-between">
      <div>
        <el-input v-model="playwith_id" style="width: 280px" placeholder="搜尋陪玩ID..." :prefix-icon="Search" class="search mr10 mb10 " />
        <el-select v-model="type" placeholder="選擇項目類型" size="large" style="width: 240px" class="mr10 mb10">
          <el-option v-for="item in options" :key="item.value" :label="item.label" :value="item.value" />
        </el-select>
        <div class="align-center mb10 mr10">
          <el-date-picker v-model="start" type="datetime" placeholder="开始时间" value-format="YYYY-MM-DD HH:mm:ss" />
          <span class="plr10 clr9fa">--</span>
          <el-date-picker v-model="end" type="datetime" placeholder="结束时间" value-format="YYYY-MM-DD HH:mm:ss" />
        </div>

      </div>
      <div class="mb10">
        <el-button class="btn2" @click="getPlaywith()">查詢</el-button>
        <el-button class="btn1" @click="reset()">重置</el-button>
      </div>
    </div>
    <div class="bgblueg ptb30 radius10 w100p mt15 plr80 justify-between">
      <div class="align-center clr3c4 flex1">
        <div>
          <div class="ft46">{{ playTotal }}</div>
          <div>佣金總額（元）</div>
        </div>
      </div>
      <div>
        <el-button class="btn2" @click="dialogShow(1)">
          <el-icon class="mr5">
            <Histogram />
          </el-icon>

          禮物金額排行榜</el-button>
        <el-button class="btn2" @click="dialogShow(2)">
          <el-icon class="mr5">
            <Histogram />
          </el-icon>

          出台金額排行榜</el-button>
      </div>

    </div>
    <div class="mt15 radius10 hidden pb15 flex1 bgwhite  flex-col w100p">
      <div class="flex1 w100p ">
        <el-table :data="playTableData" style="width: 100%" cell-class-name="tbcell" header-cell-class-name="hdcell">
          <el-table-column prop="id" label="陪玩昵稱">
            <template #default="scope">
              <span>{{ scope.row.user.name }}</span>
            </template>
          </el-table-column>
          <el-table-column prop="user" label="陪玩訊息">
            <template #default="scope">
              <span @click="showUser(scope.row.user.id)">{{ scope.row.user.name }}(ID:{{ scope.row.user.id }})</span>
            </template>
          </el-table-column>
          <el-table-column prop="money" label="縂收入" sortable>
            <template #default="scope">
              <div class="align-center" @click="dialogShow(3, scope.row)">
                <span class="mr5">{{ scope.row.money }}</span>
                <el-icon>
                  <View />
                </el-icon>
              </div>
            </template>
          </el-table-column>
          <el-table-column prop="action_text" label="類型">
            <template #default="scope">
              <span>{{ scope.row.action_text }}</span>
            </template>
          </el-table-column>

          <!-- <el-table-column prop="address" label="創建時間" /> -->
        </el-table>
      </div>
      <div class="plr20 mlauto">
        <div>小計(元): <span class="ft46 clr3c4 ml10">{{ playTotal }}</span></div>
      </div>
      <div class="plr20 mlauto mt20">
        <el-pagination v-model:current-page="currentPage1" prev-text="上一頁" next-text="下一頁" :default-page-size="15" :small="small" :disabled="disabled" :background="true" layout="prev, pager, next, jumper" :total="total1" @size-change="handleSizeChange" @current-change="handleCurrentChange" />
      </div>

    </div>
    <el-dialog @close="onClose" v-model="dialogTableVisible" :title="dialogType == 1 ? '禮物金額排行榜' : dialogType == 2 ? '出台金額排行榜' : '收入詳情'">
      <div class="plr20 justify-between ptb20" v-if="dialogType == 1 || dialogType == 2">
        <div class="align-center">
          <el-date-picker v-model="start2" type="datetime" placeholder="开始时间" value-format="YYYY-MM-DD HH:mm:ss" />
          <span class="plr10 clr9fa">--</span>
          <el-date-picker v-model="end2" type="datetime" placeholder="结束时间" value-format="YYYY-MM-DD HH:mm:ss" />
        </div>
        <div>
          <el-button class="btn2" @click="dialogSearch()">查詢</el-button>
          <el-button class="btn1" @click="dialogReset()">重置</el-button>
        </div>

      </div>
      <template v-if="dialogType != 3">
        <el-table :data="dialogTableData" cell-class-name="dialogtbcell" header-cell-class-name="dialoghdcell">
          <el-table-column property="uid" label="用戶訊息" min-width="200" />
          <el-table-column property="user" label="用戶昵稱" min-width="200">
            <template #default="scope">
              <span>{{ scope.row.user.name }}</span>
            </template>
          </el-table-column>
          <el-table-column property="address" label="用戶頭像" width="200">
            <template #default>
              <el-avatar shape="square" :size="50" :fit="'cover'" :src="url" />
            </template>

          </el-table-column>
          <el-table-column property="money" label="禮物金額" width="200" />

        </el-table>
      </template>
      <template v-if="dialogType == 3">
        <el-table :data="dialogTableData" max-height="450" cell-class-name="dialogtbcell" header-cell-class-name="dialoghdcell">
          <el-table-column property="user" label="陪玩訊息" min-width="200">
            <template #default="scope">
              <span>{{ scope.row.user.name }}(ID:{{ scope.row.user.id }})</span>
            </template>
          </el-table-column>
          <el-table-column property="user" label="陪玩昵稱" min-width="200">
            <template #default="scope">
              <span>{{ scope.row.user.name }}</span>
            </template>
          </el-table-column>
          <el-table-column property="fromuser" label="來源用戶訊息" width="200">
            <template #default="scope">
              <span>{{ scope.row.fromuser.name }}(ID:{{ scope.row.fromuser.id }})</span>
            </template>

          </el-table-column>
          <el-table-column property="money" label="來源人暱稱" width="200">
            <template #default="scope">
              <span>{{ scope.row.fromuser.name }}</span>
            </template>
          </el-table-column>
          <el-table-column property="action_text" label="佣金類型" width="200" />
          <el-table-column property="money" label="佣金" width="200" />
          <el-table-column property="created_at" label="創建時間" width="200" />




        </el-table>
      </template>

      <div class="  plr20 justify-between mt20">
        <div style="flex:1;" class="clr3c4 bold">總共{{ dialogTotal }}條</div>
        <div>小計(元): <span class="ft46 clr3c4 ml10">{{ dialogTotalNum }}</span> </div>
      </div>
    </el-dialog>
  </div>
</template>

<script setup>
import { Search } from '@element-plus/icons-vue'
import { ref, onMounted } from 'vue'
import { useRouter } from 'vue-router';
import { playwith, giftTopList, playIncomeDetail } from '@/api/statement.js'
import { inject } from 'vue';
const showUser = inject('showUser');
onMounted(() => {
  getPlaywith()
})
// 陪玩报表
const getPlaywith = () => {

  playwith({
    playwith_id: playwith_id.value,
    start: start.value,
    end: end.value,
    type: type.value,
    page: currentPage1.value
  }).then((res) => {
    console.log('陪玩报表', res)
    playTableData.value = res.list.data
    playTotal.value = res.total
    total1.value = res.list.total
  })

}
const onClose = () => {
  start2.value = ''
  end2.value = ''
}
const reset = () => {
  playwith_id.value = ''
  type.value = ''
  start.value = ''
  end.value = ''
  getPlaywith()
}
const dialogSearch = () => {
  if (dialogType.value == 1) {
    getGiftTopList(1)
  }
  if (dialogType.value == 2) {
    getGiftTopList(2)
  }

}
const dialogReset = () => {
  start2.value = ''
  end2.value = ''
  if (dialogType.value == 1) {
    getGiftTopList(1)
  }
  if (dialogType.value == 2) {
    getGiftTopList(2)
  }

}
const start2 = ref('')
const end2 = ref('')
// 出柜 / 礼物排行榜
const getGiftTopList = (type) => {
  giftTopList({
    type: type == 1 ? '3' : '2',
    start: start2.value,
    end: end2.value
  }).then((res) => {
    console.log('出柜 / 礼物排行榜', res)
    dialogTableData.value = res.list.data
    dialogTotal.value = res.list.total
    dialogTotalNum.value = res.total
  })
}
//陪玩收入详情
const getPlayIncomeDetail = () => {
  playIncomeDetail({
    uid: currentUid.value,
    action:action.value,
    start: start2.value,
    end: end2.value
  }).then((res) => {
    console.log('陪玩收入详情', res)
    dialogTableData.value = res.list.data
    dialogTotal.value = res.list.total
  })
}

const currentUid = ref('')

const playwith_id = ref('')
const type = ref('')
const start = ref('')
const end = ref('')

const options = [
  {
    value: '2',
    label: '服務單',
  },
  {
    value: '3',
    label: '打賞',
  }
]
const playTableData = ref([])
const playTotal = ref(0)
const dialogTableData = ref([])
const dialogTotal = ref(0)
const dialogTotalNum = ref('')


const currentPage1 = ref(1)
const total1 = ref(0)
const currentTotalMoney = ref(0)
const handleCurrentChange1 = (val) => {
  console.log(`current page: ${val}`)
  getPlaywith()
}
const action = ref(0)
const dialogType = ref(1)
const dialogTableVisible = ref(false)
const dialogShow = (type, row) => {
  dialogType.value = type
  dialogTableVisible.value = true
  if (type == 1 || type == 2) {
    getGiftTopList(type)
  }
  if (type == 3) {
    currentUid.value = row.uid
    action.value = row.action
    dialogTotalNum.value = row.money
    getPlayIncomeDetail()
  }
}
const router = useRouter()
const toAdd = () => {
  console.log(router)
  router.push({ path: 'addNotice' })
}
const edit = (type) => {
  router.push({ path: 'signEdit' })
}
</script>

<style lang="scss" scoped>
.search {
  overflow: hidden;
  border-radius: 10px !important;
  // padding: 5px 0px;
}
</style>
<style></style>
 